package com.eztalks.demo;

import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;

public class CreateQueue implements MessageListener {
	static ActiveMQConnectionFactory activeMQConnectionFactory = null;
	static Connection proConnetiong = null;
	static Connection connection1 = null;
	static MessageProducer createProducer = null;
	static {
		try {
			activeMQConnectionFactory = new ActiveMQConnectionFactory("failover:(tcp://192.168.32.201:61616)");
			proConnetiong = activeMQConnectionFactory.createConnection("root", "123456");
			connection1 = activeMQConnectionFactory.createConnection("root", "123456");
			connection1.start();
			proConnetiong.start();

			Session session = proConnetiong.createSession(false, Session.AUTO_ACKNOWLEDGE);
			createProducer = session.createProducer(null);
			createProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) throws Exception {
		try {
			Session session1 = connection1.createSession(false, Session.AUTO_ACKNOWLEDGE);
			Queue custormer = session1.createQueue("vodserver_pushstream_notify");
			MessageConsumer createConsumer = session1.createConsumer(custormer);
			createConsumer.setMessageListener(new CreateQueue());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void onMessage(Message message) {
		try {
			String jmsCorrelationID = message.getJMSCorrelationID();
			Destination jmsReplyTo = message.getJMSReplyTo();

			System.out.println("收到消息：jmsjmsCorrelationID=" + jmsCorrelationID + ", jmsReplyTo=" + jmsReplyTo);

			message.setJMSCorrelationID(jmsCorrelationID);
			createProducer.send(jmsReplyTo, message);

		} catch (JMSException e) {
			e.printStackTrace();
		}
	}
}
